package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.LinkedProblem;

/**
 * 翻转链表 see LC234_reverse
 *
 * @author tzp
 * @since 2020/10/15
 */
public class LC206 implements LinkedProblem {
    //    public ListNode reverseList(ListNode head) {
//        ListNode dummyRoot = new ListNode(-1);
//        while (head != null) {
//            ListNode tmpNext = head.next;
//            head.next = dummyRoot.next;
//            dummyRoot.next = head;
//            head = tmpNext;
//        }
//        return dummyRoot.next;
//    }
    public ListNode reverseList(ListNode head) {
        ListNode newHead = null;
        while (head != null) {
            ListNode tmp = head.next;
            head.next = newHead;
            newHead = head;
            head = tmp;
        }
        return newHead;
    }

    public static void main(String[] args) {
        ListNode a = ListNode.arrayToListNode(new int[]{1, 2, 3, 4});
        System.out.println(new LC206().reverseList(a));
    }
}
